/**
 * Copyright (C) 2012 The SkyTvOS Project
 *
 * Version     Date           Author
 * ─────────────────────────────────────
 *           2015-2-11         yellowlgx
 *
 */

package com.skyworth.util;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Shader;
import android.widget.TextView;

/**
 * @ClassName SkyShaderUtil
 * @Description <p>
 *              此类为渲染工具类，可以让一个textView或者一个Bitmap产生颜色上的渲染效果，例如，想要让一个TextView从上到下透明度产生一个渐变：
 *              textView.setShader(0, 0, 0, textView.getHeight(), Color.WHITE, Color.TRANSPARENT);
 *              其他详细的用法，该类里都有详细的说明；
 *              </p>
 * @author yellowlgx
 * @date 2015-2-11
 * @version TODO (write something)
 */
public class SkyShaderUtil
{
    private static SkyShaderUtil instance;

    public static SkyShaderUtil getInstance()
    {
        if (instance == null)
        {
            synchronized (SkyShaderUtil.class)
            {
                if (instance == null)
                    instance = new SkyShaderUtil();
            }
        }
        return instance;
    }

    /**
     * 设置该模块是否需要透明度渐变
     * 
     * @param x0
     *            渐变起始X坐标，默认是从0开始
     * @param y0
     *            渐变起始Y坐标，默认是从0开始
     * @param x1
     *            渐变结束X坐标，根据实际情况来定
     * @param y1
     *            渐变结束Y坐标，根据实际情况来定
     * @param color0
     *            渐变起始颜色，int型
     * @param color1
     *            渐变结束颜色，int型
     * @date 2015-2-11
     */
    public TextView getShaderTextView(TextView view, float x0, float y0, float x1, float y1,
            int color0, int color1)
    {
        Paint mPaint = view.getPaint();
        mPaint.setShader(new LinearGradient(x0, y0, x1, y1, color0, color1, Shader.TileMode.CLAMP));
        return view;
    }

    /**
     * 设置该模块是否需要透明度渐变,同上，唯一的差别的是这个可以多个颜色渐变
     * 
     * @param x0
     * @param y0
     * @param x1
     * @param y1
     *            同上
     * @param colors
     *            渐变起始颜色数组
     * @date 2015-2-11
     */
    public TextView getShaderTextView(TextView view, float x0, float y0, float x1, float y1,
            int[] colors)
    {
        Paint mPaint = view.getPaint();
        mPaint.setShader(new LinearGradient(x0, y0, x1, y1, colors, null, Shader.TileMode.CLAMP));
        return view;
    }

    /**
     * 设置该模块是否需要透明度渐变
     * 
     * @param x0
     * @param y0
     * @param x1
     * @param y1
     * @param color0
     * @param color1
     *            同上
     * @param res
     *            图片资源，Bitmap
     * @date 2015-2-11
     */
    public Bitmap getShaderBitmap(float x0, float y0, float x1, float y1, int color0, int color1,
            Bitmap res)
    {
        Bitmap overlay = Bitmap.createBitmap(res.getWidth(), res.getHeight(),
                Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(overlay);
        Paint paint = new Paint();
        paint.setFlags(Paint.FILTER_BITMAP_FLAG);
        paint.setShader(new LinearGradient(x0, y0, x1, y1, color0, color1, Shader.TileMode.CLAMP));
        canvas.drawBitmap(res, 0, 0, null);
        PorterDuffXfermode mXfermode = new PorterDuffXfermode(PorterDuff.Mode.DST_IN);
        paint.setXfermode(mXfermode);
        canvas.drawRect(0, 0, res.getWidth(), res.getHeight(), paint);
        return overlay;
    }

    /**
     * 设置该模块是否需要透明度渐变,同上，唯一的差别的是这个可以多个颜色渐变
     * 
     * @param x0
     * @param y0
     * @param x1
     * @param y1
     *            同上
     * @param colors
     *            渐变起始颜色数组
     * @param res
     *            图片资源，Bitmap
     * @date 2015-2-11
     */
    public Bitmap getShaderBitmap(float x0, float y0, float x1, float y1, int[] colors, Bitmap res)
    {
        Bitmap overlay = Bitmap.createBitmap(res.getWidth(), res.getHeight(),
                Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(overlay);
        Paint paint = new Paint();
        paint.setFlags(Paint.FILTER_BITMAP_FLAG);
        paint.setShader(new LinearGradient(x0, y0, x1, y1, colors, null, Shader.TileMode.CLAMP));
        canvas.drawBitmap(res, 0, 0, null);
        PorterDuffXfermode mXfermode = new PorterDuffXfermode(PorterDuff.Mode.DST_IN);
        paint.setXfermode(mXfermode);
        canvas.drawRect(0, 0, res.getWidth(), res.getHeight(), paint);
        return overlay;
    }
}
